import React  from 'react';
import {  withRouter } from 'react-router-dom';
import { observer,inject } from 'mobx-react';
import { SearchBar, Icon,Tools} from 'weaver-mobile';
import { SearchAdvanced ,SearchPage} from 'weaver-mobile-page';
import {CommonText} from '../comText/text'
import GovernConstant from "../../../util/index";
const getLabel = Tools.getLabel;
const commonText = new CommonText();

@inject('domSearchStore')
@withRouter
@observer
/*
 * @info: 顶部搜索组件
 * @Author: lusx
 * @Date: 2019-12-01 11:13:16
 * @Last Modified by: lusx
 * @Last Modified time: 2019-10-17 19:10:03
 */
export default class ComSearchBar extends React.Component {
  componentWillMount() {
    const {domSearchStore:{setSearchRoute},match:{path}} = this.props;
    setSearchRoute(path)
  }
  componentDidMount() {
    const pattern = /advanced/i;
    if(!!pattern.test(this.props.location.pathname)){//注：在路由包含 -advanced-时，请求列表接口的入参多了deadtype 需改正
      return false;
    }else {
      const {domSearchStore,tabKey,initDatas,getDataParams,apisFun,location:{pathname}} = this.props;
      domSearchStore.initDatas({initDatas,getDataParams,apisFun,pathname});
      //domSearchStore.getTabKey(tabKey);//19.1.21该方法无用
    }
  }
  componentWillReceiveProps(nextProps){
    let isgo = GovernConstant.getUrlCharCount(nextProps.history.location.pathname) < 3;
    if(isgo && this.props.location != nextProps.location && !nextProps.location.pathname.includes('idadvanced') ){//19.1.21解决高级搜索有人力资源时，高级搜索重新渲染
        const {domSearchStore,tabKey,initDatas,getDataParams,apisFun,location:{pathname}} = nextProps;
        domSearchStore.initDatas({initDatas,getDataParams,apisFun,pathname});
        //domSearchStore.getTabKey(tabKey);//19.1.21该方法无用
    }
  }
  componentWillUnmount(){
    const {domSearchStore:{clearState}} = this.props;
    clearState();
  }
    render(){
        const { domSearchStore,history} = this.props;
        const { searchAdvancedStore,submitAdvanced,searchKey,searchPageStore ,updateSearchGovSubject,doSearchByKeyword,clearSearchHistory } = domSearchStore;
        const PLACEHOLDER = getLabel(commonText.PLACEHOLDER[0],commonText.PLACEHOLDER[1]);
        return(
            <div className = "g-comSearchBar">
                <SearchBar
                    className="g-searchbar"
                    value = {searchKey.keyword ? searchKey.keyword : ''}
                    placeholder={PLACEHOLDER}
                    onFocus={()=>history.push(searchPageStore.path)}
                    showExtraButton = {true}
                    extraButton={[
                    { key: 'advanced', show: <Icon key="filter-o" type="filter-o" /> },
                    ]}
                    onExtraButtonClick={() => history.push(searchAdvancedStore.path)}
                />
                <SearchPage
                    store={searchPageStore}
                    type={1}
                    nameKey="name"
                    rowKey="key"
                    onChange={(v) => domSearchStore.updateSearchGovSubject(v)}
                    onSubmit={(v) => {
                      domSearchStore.doSearchByKeyword(typeof(v) === 'string' ? v : v.name)
                    }}
                    onClear={()=>{
                      domSearchStore.clearSearchHistory();
                    }}
                  />
                 <SearchAdvanced
                    store={searchAdvancedStore}
                    onSubmit={()=>{submitAdvanced();}}
                />
            </div>
        )
    }
}
